package com.campus.merchant.service;

import com.campus.merchant.handler.NativeWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * WebSocket消息控制器
 * 基于原生WebSocket协议的消息处理
 */
@Service
@Slf4j
public class WebSocketService {
    
    @Autowired
    private NativeWebSocketHandler nativeWebSocketHandler;



    /**
     * 向指定商户发送订单通知
     * 基于原生WebSocket协议
     * 
     * @param merchantId 商户ID
     * @param orderMessage 订单消息内容
     */
    public void sendOrderNotification(Integer merchantId, String orderMessage) {
        log.info("向商户{}发送订单通知: {}", merchantId, orderMessage);
        nativeWebSocketHandler.sendOrderNotification(merchantId, orderMessage);
    }

    /**
     * 广播系统通知给所有在线商户
     * 基于原生WebSocket协议
     * 
     * @param message 系统通知消息
     */
    public void broadcastSystemNotification(String message) {
        log.info("广播系统通知: {}", message);
        nativeWebSocketHandler.broadcastSystemNotification(message);
    }
    
    /**
     * 获取WebSocket连接统计信息
     * 
     * @return 连接统计信息
     */
    public String getConnectionStats() {
        int onlineMerchants = nativeWebSocketHandler.getOnlineMerchantCount();
        int totalConnections = nativeWebSocketHandler.getTotalConnectionCount();
        return String.format("在线商户数: %d, 总连接数: %d", onlineMerchants, totalConnections);
    }
}